package com.yuncheng.spcyApi.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.yuncheng.spcyApi.constant.common.BusinessConstant;
import com.yuncheng.spcyApi.constant.common.SpcyCommonConstant;
import com.yuncheng.spcyApi.constant.common.SpcyConstant;
import com.yuncheng.spcyApi.entity.SpcySqsxWjjl;
import com.yuncheng.spcyApi.mapper.SpcySqsxWjjlMapper;
import com.yuncheng.spcyApi.service.ISpcySqsxWjjlService;
import com.yuncheng.spcyApi.utils.GetUuIdUtils;
import com.yuncheng.vo.CurrentUserVo;
import com.yuncheng.spcyApi.vo.wj.WjjlVo;
import org.apache.commons.collections4.ListUtils;
import org.springframework.stereotype.Service;

import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description: 审评查验-文件记录
 * @Author: csx
 * @Date: 2023-03-03
 * @Version: V1.0
 */
@Service
public class SpcySqsxWjjlServiceImpl extends BaseServiceImpl<SpcySqsxWjjlMapper, SpcySqsxWjjl> implements ISpcySqsxWjjlService {

    @Resource
    private SpcySqsxWjjlMapper spcySqsxWjjlMapper;

    @Override
    public List<WjjlVo> findWjjlList(WjjlVo wjjlVo) {
        return spcySqsxWjjlMapper.findWjjlList(wjjlVo);
    }

    @Override
    public List<WjjlVo> findBtWjjlList(String fpid, String fywflcode, String mlmc, String sftscl) {
        return spcySqsxWjjlMapper.findBtWjjlList(fpid, fywflcode, mlmc, sftscl);
    }

    @Override
    public List<SpcySqsxWjjl> findUserQmList(String userid) {
        List<SpcySqsxWjjl> list = spcySqsxWjjlMapper.selectList(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, userid)
                .eq(SpcySqsxWjjl::getFwjlx1, BusinessConstant.USER_QM));

        return list;
    }

    @Override
    public List<SpcySqsxWjjl> findUserPhotoList(String userid) {
        List<SpcySqsxWjjl> list = spcySqsxWjjlMapper.selectList(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, userid)
                .eq(SpcySqsxWjjl::getFwjlx1, BusinessConstant.USER_PHOTO));

        return list;
    }

    @Override
    public List<SpcySqsxWjjl> findUserJcyZjList(String userid) {
        List<SpcySqsxWjjl> list = spcySqsxWjjlMapper.selectList(new LambdaQueryWrapper<SpcySqsxWjjl>()
                .eq(SpcySqsxWjjl::getFpid, userid)
                .eq(SpcySqsxWjjl::getFwjlx1, BusinessConstant.USER_JCY_ZJ));

        return list;
    }

    @Override
    public SpcySqsxWjjl downMyUserQm(String id, CurrentUserVo currentUser) {
        String userid = "";
        if (StringUtils.isNotBlank(id)){
            if (id.contains(",")){
                String[] ids = id.split(",");
                userid = ids[0];
            }else {
                userid = id;
            }
        }else {
            userid = currentUser.getId();
        }
        List<SpcySqsxWjjl> qmList = findUserQmList(userid);

        /*String url = "";
        if (CollectionUtils.isNotEmpty(qmjl)) {
            url = qmjl.get(0).getFcflj();
        }*/
        if (CollectionUtils.isNotEmpty(qmList)) {
            return qmList.get(0);
        }else {
            return null;
        }
    }

    @Override
    public SpcySqsxWjjl downMyUserPhoto(String id, CurrentUserVo currentUser) {
        String userid = "";
        if (StringUtils.isNotBlank(id)){
            if (id.contains(",")){
                String[] ids = id.split(",");
                userid = ids[0];
            }else {
                userid = id;
            }
        }else {
            userid = currentUser.getId();
        }
        List<SpcySqsxWjjl> photoList = findUserPhotoList(userid);

        if (CollectionUtils.isNotEmpty(photoList)) {
            return photoList.get(0);
        }else {
            return null;
        }
    }

    @Override
    public SpcySqsxWjjl downMyUserJcyZj(String id, CurrentUserVo currentUser) {
        String userid = "";
        if (StringUtils.isNotBlank(id)){
            if (id.contains(",")){
                String[] ids = id.split(",");
                userid = ids[0];
            }else {
                userid = id;
            }
        }else {
            userid = currentUser.getId();
        }
        List<SpcySqsxWjjl> photoList = findUserJcyZjList(userid);

        if (CollectionUtils.isNotEmpty(photoList)) {
            return photoList.get(0);
        }else {
            return null;
        }
    }

    @Override
    public void updateWjjlByWjlx5(List<String> idsList) {
        if (CollectionUtil.isEmpty(idsList)){
            return;
        }

        // 按15为一组进行更新
        List<List<String>> partition = ListUtils.partition(idsList, 20);

        partition.forEach(item -> {
            LambdaUpdateWrapper<SpcySqsxWjjl> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.in(SpcySqsxWjjl::getId, item);

            SpcySqsxWjjl updateBean = new SpcySqsxWjjl();
            updateBean.setFwjlx5(SpcyCommonConstant.SQSX_WJJL_TYPE5_SFYTS_YTS);

            update(updateBean, updateWrapper);
        });

    }

    @Override
    public List<SpcySqsxWjjl> getWjjlListInZzz(String pid, String type1, boolean isZzz) {
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid, pid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type1);
        if (isZzz) {
            queryWrapper.eq(SpcySqsxWjjl::getFwjlx3, SpcyCommonConstant.SQSX_WJJL_TYPE3_ZZZ);
        }
        return list(queryWrapper);
    }

    @Override
    public List<SpcySqsxWjjl> getTbwdWjjlListByFwjlx2(String pid, String type1, boolean orderDescCreateTime) {
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid, pid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type1);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx2, SpcyCommonConstant.SQSX_WJJL_TYPE2_TBWDZ);
        if (orderDescCreateTime) {
            queryWrapper.orderByDesc(SpcySqsxWjjl::getCreateTime);
        }
        return list(queryWrapper);
    }

    @Override
    public List<SpcySqsxWjjl> getTbWcWjjlListByFwjlx2(String pid, String type1, boolean orderDescCreateTime) {
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid, pid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type1);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx2, SpcyCommonConstant.SQSX_WJJL_TYPE2_TBWDWC);
        if (orderDescCreateTime) {
            queryWrapper.orderByDesc(SpcySqsxWjjl::getCreateTime);
        }
        return list(queryWrapper);
    }

    @Override
    public List<SpcySqsxWjjl> getHistoryWjjlList(String pid, String type1, boolean orderDescCreateTime) {
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid, pid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type1);
        if (orderDescCreateTime) {
            queryWrapper.orderByDesc(SpcySqsxWjjl::getCreateTime);
        }
        return list(queryWrapper);
    }

    @Override
    public List<SpcySqsxWjjl> getWjjlListByPidAndType(String pid, String type1, boolean isOrder) {
        LambdaQueryWrapper<SpcySqsxWjjl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SpcySqsxWjjl::getFpid, pid);
        queryWrapper.eq(SpcySqsxWjjl::getFwjlx1, type1);
        if (isOrder) {
            queryWrapper.orderByAsc(SpcySqsxWjjl::getCreateTime);
        }
        return list(queryWrapper);
    }

    @Override
    public void updateWjjlByTbwdz(String pid, String type1) {
        LambdaUpdateWrapper<SpcySqsxWjjl> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxWjjl::getFpid, pid);
        updateWrapper.eq(SpcySqsxWjjl::getFwjlx1, type1);
        updateWrapper.eq(SpcySqsxWjjl::getFwjlx2, SpcyCommonConstant.SQSX_WJJL_TYPE2_TBWDZ);

        SpcySqsxWjjl updateBean = new SpcySqsxWjjl();
        updateBean.setFwjlx2(SpcyCommonConstant.SQSX_WJJL_TYPE2_TBWDWC);

        update(updateBean, updateWrapper);
    }

    @Override
    public void updateWjjlByFdxzt(String id) {
        if (StringUtils.isBlank(id)){
            return;
        }
        LambdaUpdateWrapper<SpcySqsxWjjl> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxWjjl::getId, id);

        SpcySqsxWjjl updateBean = new SpcySqsxWjjl();
        updateBean.setFdxzt(SpcyConstant.WJJL_ZT_DX);

        update(updateBean, updateWrapper);
    }

    @Override
    public void updateWjjlByFyxzt(String id) {
        if (StringUtils.isBlank(id)){
            return;
        }
        LambdaUpdateWrapper<SpcySqsxWjjl> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(SpcySqsxWjjl::getId, id);

        SpcySqsxWjjl updateBean = new SpcySqsxWjjl();
        updateBean.setFyxzt(SpcyConstant.WJJL_ZT_YX);

        update(updateBean, updateWrapper);
    }

    @Override
    public SpcySqsxWjjl genWjjlByScpdf(SpcySqsxWjjl wjjl, String fcflj, String fjdcflj, String fwjccmc) {
        wjjl.setFcflj(fcflj);
        wjjl.setFjdcflj(fjdcflj);
        wjjl.setFwjlx(".pdf");
        wjjl.setFwjccmc(fwjccmc);
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_YSCPDF);

        return wjjl;
    }

    @Override
    public SpcySqsxWjjl genWjjlByPdfGz(SpcySqsxWjjl wjjl, String fcflj, String fwjccmc) {
        wjjl.setFcflj(fcflj);
        wjjl.setFwjccmc(fwjccmc);
        wjjl.setFgzzt(SpcyConstant.WJJL_ZT_GZ); // 盖章
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_YWC);

        return wjjl;
    }

    @Override
    public SpcySqsxWjjl genWjjlByScpdfAndGz(SpcySqsxWjjl wjjl, String fcflj, String fjdcflj, String fwjccmc, boolean isGz) {
        wjjl.setFcflj(fcflj);
        wjjl.setFjdcflj(fjdcflj);
        wjjl.setFwjlx(".pdf");
        wjjl.setFwjccmc(fwjccmc);

        if (isGz) {
            wjjl.setFgzzt(SpcyConstant.WJJL_ZT_GZ); // 盖章
            wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_YWC);
        }else {
            wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_YSCPDF);
        }

        return wjjl;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void batchInsertBhbsxWjjlList(List<SpcySqsxWjjl> bhbSxWjList) {
        if (CollectionUtil.isEmpty(bhbSxWjList)){
            return;
        }

        List<List<SpcySqsxWjjl>> partitionWjList = ListUtils.partition(bhbSxWjList, 30);
        partitionWjList.forEach(item -> {
            saveBatch(item);
        });
    }

    @Override
    public List<SpcySqsxWjjl> findWjjlListByFwjlx1(List<String> list, String fpid) {
        return spcySqsxWjjlMapper.findWjjlListByFwjlx1(list, fpid);
    }

    @Override
    public List<SpcySqsxWjjl> findWjjlListByFwjlx1AndFwjlx7(List<String> list, String fpid, String fwjlx7) {
        return spcySqsxWjjlMapper.findWjjlListByFwjlx1AndFwjlx7(list, fpid, fwjlx7);
    }

    @Override
    public List<SpcySqsxWjjl> findWjjlJbxxListByFwjlx1(List<String> list, String fpid) {
        return spcySqsxWjjlMapper.findWjjlJbxxListByFwjlx1(list, fpid);
    }

    @Override
    public void intiAndSaveInitSqsxWjjl(String sqsxid, String type, String fwjlx, String wjxsmc, String wjccmc, String cflj) {
       this.initSaveSqsxWjjl(sqsxid, type, fwjlx, wjxsmc, wjccmc, cflj, "");
    }

    @Override
    public SpcySqsxWjjl initSaveSqsxWjjl(String sqsxid, String type, String fwjlx, String wjxsmc, String wjccmc, String cflj, String fwjlx7) {
        SpcySqsxWjjl newWjjl = new SpcySqsxWjjl();
        newWjjl.setId(GetUuIdUtils.ReplaceUuId());
        newWjjl.setFpid(sqsxid);
        newWjjl.setFwjlx(fwjlx);

        newWjjl.setFwjlx1(type);

        newWjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_YWC2);
        newWjjl.setFwjlx4(SpcyCommonConstant.SQSX_WJJL_TYPE4_SDSC);

        newWjjl.setFwjxsmc(wjxsmc);
        newWjjl.setFwjccmc(wjccmc);
        newWjjl.setFcflj(cflj);
        newWjjl.setFwjnr("");
        newWjjl.setFbusid("");
        newWjjl.setFbusmc("");
        newWjjl.setFwjlx7(fwjlx7);

        if (save(newWjjl)){
            return newWjjl;
        }
        return null;
    }

    @Override
    public List<String> findFwjlx2ByFpidAndFwjlx1(String fpid, String fwjlx1) {
        return spcySqsxWjjlMapper.findFwjlx2ByFpidAndFwjlx1(fpid, fwjlx1);
    }

    @Override
    public SpcySqsxWjjl genDefinedWjjlWithUnReal(String fpid, String type, String fwjlx, String wjxsmc) {
        SpcySqsxWjjl wjjl = new SpcySqsxWjjl();
        wjjl.setId(type+"@"+fpid);
        wjjl.setFwjxsmc(wjxsmc);
        wjjl.setFwjlx(fwjlx);
        wjjl.setFpid(fpid);
        wjjl.setFwjlx1(type);

        wjjl.setFwjccmc(wjjl.getFwjxsmc() + wjjl.getFwjlx());
        wjjl.setFcflj(wjjl.getFwjccmc());
        wjjl.setFwjlx7(fpid);

        wjjl.setFwjlx2(wjjl.getFwjlx1());
        wjjl.setFwjlx3(SpcyCommonConstant.SQSX_WJJL_TYPE3_YWC2);

        return wjjl;
    }

    @Override
    public List<String> verifyExistsWjjl(String fpid, String type) {
        return spcySqsxWjjlMapper.verifyExistsWjjl(fpid, type);
    }

    @Override
    public List<String> verifySfQzWjjl(String fpid, String fwjlx1) {
        return spcySqsxWjjlMapper.verifySfQzWjjl(fpid, fwjlx1, SpcyCommonConstant.SQSX_WJJL_TYPE4_SDSC, "已签字");
    }

}
